﻿using NetworkLib;
using NetworkLib.Tcp;
using NetworkLib.Utilities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace PBO.Network
{
    public class BattleClient : NetworkClient, IProtocolInterpreter
    {
        protected TcpNetworkStrategy strategy;

        public BattleClient(string ip, int port)
        {
            this.strategy = new TcpNetworkStrategy();
            strategy.ServerIP = ip;
            strategy.Port = port;
            strategy.Sync = true;

            base.NetworkStrategy = strategy;
            base._interpreter = this;
        }

        #region ITopClient

        public void SetPort(int port)
        {
            strategy.Port = port;
        }

        public event Action<int, int, ByteArray> ReceiveData;
        protected virtual void OnReceiveData(int sessionID, int method, ByteArray data)
        {
            if (this.ReceiveData != null) this.ReceiveData(sessionID, method, data);
        }

        #endregion

        #region IProtocolInterpreter

        public bool InterpretMessage(int sessionID, ByteArray byteArray)
        {
            byteArray.BypassHeader();
            int method = byteArray.ReadInt();
            OnReceiveData(sessionID, method, byteArray);
            byteArray.Rewind();
            return false;
        }

        #endregion

    }
}
